<?php
namespace Admin\Controller;

use Think\PageAdmin;

/**
 * 教师管理控制器
 */
class ProjectController extends BaseController
{

    /**
     * 初始化
     *
     * @return [type] [description]
     */
    public function _initialize()
    {
        parent::_initialize();
    }

    

    public function add()
    {
        $info = $this->get_info();

        $this->seotitle = "添加项目";
        $this->assign('info', $info);
        $this->display();
    }

    public function index(){
        
        $info = $this->get_info();

        $this->seotitle = "项目管理";
        $this->assign('info', $info);
        $this->display();

    }


    /**
     * 保存
     * @return [type] [description]
     */
    public function save()
    {
        $action = I('get.action', '', 'trim');
        $params = I('post.');
        //var_dump(strtotime($params['end_time']));die;
        if ($params['classname'] == '') {
            $this->error('请输入栏目名称');
        }

        

        if ($params['pid'] < '0') {
            $this->error('请选择所属项目');
        }


        $pinfo = M('project')->where(['id'=>$params['pid']])->find();
        $path = $pinfo['path'];

        // 新增
        if ($action == 'add') {
            $id = M('project')->add([
                        'classname' => trim($params['classname']),
                        'pid'       => trim($params['pid']),                        
                        
                    ]);

            if($params['pid'] == 0){
                $path = '0,';
            }

            M('project')->where(['id'=>$id])->save(['path'=>$path.$id.',']);

            $this->success('操作成功', U('index'));
        }

        // 编辑保存
        elseif ($action == 'edit') {
            $id = I('post.id', 0, 'intval');

            M('project')->where(['id' => $id])->save([
                'classname'           => trim($params['classname']),
                'pid'       => trim($params['pid']),
                'path'      => $path.$id.',',
            ]);

            $this->success('操作成功', U('index'));
        }
    }

    public function edit()
    {
        $id = I('get.id', 0, 'intval');

        if (empty($id) ||
            !$info = M('task')->where(['id' => $id])->find()) {
            $this->error('参数错误');
        }
        //var_dump($info);die;
        $this->seotitle = '任务编辑';
        $this->info     = $info;
        $this->display();
    }

    public function delete()
    {
        $id = I('get.id', 0, 'intval');

        //有子集不可删除
        if(M('project')->where(['pid'=>$id])->find()){
            $this->error('当前不可删除');
        }

        M('project')->where(['id' => $id])->delete();

        $this->success('操作成功');
    }

    public function get_info(){
        $info = M("project")->where(['pid' => 0])->field('id,classname value')->select();
        foreach ($info as $k => $v) {
            $child = M("project")->where(['pid' => $v['id']])->field('id,classname value')->select();
            foreach ($child as $key1 => $value1) {
                $child[$key1]['title'] = $v['value'] . ',' . $value1['value'];
            }

            $info[$k]['childs'] = $child;
            $info[$k]['title']  = $v['value'];
            foreach ($child as $key => $value) {
                $childs = M("project")->where(['pid' => $value['id']])->field('id,classname value')->select();
                foreach ($childs as $key2 => $value2) {
                    $childs[$key2]['title'] = $value['title'] . ',' . $value2['value'];
                }

                $info[$k]['childs'][$key]["childs"] = $childs;
            }
        }

        foreach ($info as $key => $value) {
            $info[$key]['childs'] = array_filter($info[$key]['childs']);
            if ($info[$key]['childs'] && count($info[$key]['childs']) > 0) {
                $info[$key]['flag'] = 1;
            } else {
                $info[$key]['flag'] = 0;
            }
            foreach ($info[$key]['childs'] as $key1 => $value1) {
                if ($info[$key]['childs'][$key1]['childs'] && count($info[$key]['childs'][$key1]['childs']) > 0) {
                    $info[$key]['childs'][$key1]['flag'] = 1;
                } else {
                    $info[$key]['childs'][$key1]['flag'] = 0;
                }
            }
        }

        return $info;
    }

}
